package com.coolpad.job.device.util;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.net.NetUtil;
import cn.hutool.core.util.IdUtil;

public class IdWorker {

    private static long workerId=0L;
    private static long dataCenterId=1L;
    private static Snowflake snowflake= IdUtil.createSnowflake(workerId, dataCenterId);

    static{
        try{
            workerId= NetUtil.ipv4ToLong(NetUtil.getLocalhostStr());
        }catch(Exception e){
            workerId=NetUtil.getLocalhostStr().hashCode();
        }
    }

    public static synchronized long nextId(long workerId,long datacenterId){
        Snowflake snowflake=IdUtil.createSnowflake(workerId, datacenterId);
        return snowflake.nextId();
    }

    public static synchronized long nextId(){
        return snowflake.nextId();
    }

    public static void main(String[] args) {
        int count = 0;
        int num = 3;
        for (int i = 0; i < 30; i ++){
            System.out.println(count = ++count % num);
        }
    }
}
